package com.woniuxy.service.impl;

import com.woniuxy.dao.*;
import com.woniuxy.pojo.Address;
import com.woniuxy.pojo.Cart;
import com.woniuxy.pojo.Order;
import com.woniuxy.pojo.OrderItem;
import com.woniuxy.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.UUID;

/**
 * @Author 罗蔚
 * @Date 2022/3/28 1:41
 * @Description TODO
 */
@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private OrderItemDao orderItemDao;
    @Autowired
    private CartDao cartDao;
    @Autowired
    private AddressDao addressDao;


    @Override
    public List<Cart> findShopCartByStatus(int uid) {
        return cartDao.findShopCartByStatus(uid);
    }

    @Override
    public int writeOrderData(int uid, int addressId, String paytype, BigDecimal allPrice) {
        Order order = new Order();

        String orderno = UUID.randomUUID().toString().replace("-", "");
        int userid = uid;
        LocalDateTime localDateTime = LocalDateTime.now();
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String ordertime = localDateTime.format(dateTimeFormatter);

        Address _address = addressDao.findByIdAndUid(addressId, uid);

        String accept = _address.getAccept();
        String telphone = _address.getTelphone();
        String address = _address.getProvince() + _address.getCity() + _address.getArea() + _address.getAddress();

        //获取money
        BigDecimal money = allPrice;

        //status, "0"是代付款, "1"是已付款
        String status = "0";

        //将字段存到order
        order.setOrderno(orderno);
        order.setUserid(userid);
        order.setOrdertime(ordertime);
        order.setAccept(accept);
        order.setTelphone(telphone);
        order.setAddress(address);
        order.setMoney(money);
        order.setPaytype(paytype);
        order.setStatus(status);

        orderDao.insertNewOrder(order);

        //查询出新增的order, 得到orderid
        int orderid = orderDao.findOrderIdByOrderno(orderno);

        //再查询购物车表中, status等于1 的数据
        List<Cart> list = cartDao.findReadyByUid(userid);
        for (Cart cart : list) {
            //将数据存到order_item表中
            OrderItem orderItem = new OrderItem();
            orderItem.setOrderid(orderid);
            orderItem.setGoodsid(cart.getGoodsid());
            orderItem.setNums(cart.getNums());
            orderItem.setPrice(cart.getPrice());

            orderItemDao.insertNew(orderItem);
        }

        //再将购物车中的数据删除
        cartDao.deleteByUidAndStatus(userid);
        return orderid;
    }

    @Override
    public Order findNoAndMoneyAndPaltypeByOrderId(int id, int userid) {
        return orderDao.findNoAndMoneyAndPaltypeByOrderId(id, userid);
    }

    @Override
    public List<Order> findAll(int uid) {
        return orderDao.findAll(uid);
    }

    @Override
    public List<Order> findFiveByDate(int uid) {
        return orderDao.findFiveByDate(uid);
    }

    @Override
    public Order findOrderDetail(int id, int userid) {
        return orderDao.findOrderDetail(id, userid);
    }

    @Override
    public void deleteOrder(String orderno) {
        orderDao.deleteOrder(orderno);
    }
}
